package com.recharge.api.service.impl;

import com.recharge.api.dto.request.CommissionRequest;
import com.recharge.api.mapper.CommissionMapper;
import com.recharge.api.model.CommissionRates;
import com.recharge.api.service.CommissionService;
import com.recharge.api.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class CommissionServiceImpl implements CommissionService {
    @Autowired
    private CommissionMapper commissionMapper;
    @Override
    public Result getCommissionList() {
        try {
            List<CommissionRates> commissionRates = commissionMapper.selectList(null);
            log.info("commissionRates: {}", commissionRates);
            return Result.success(commissionRates);
        } catch (Exception e) {
            log.error("getCommissionList: ", e.getMessage());
            e.printStackTrace();
        }

        return Result.build(1,"Query failed");

    }

    @Override
    public Result updateCommission(CommissionRequest commissionRequest) {
        try {
            CommissionRates commissionRates = new CommissionRates();
            commissionRates.setId(commissionRequest.getId());
            commissionRates.setCommissionValue(commissionRequest.getCommissionValue());
            commissionRates.setMaxAmount(commissionRequest.getMaxAmount());
            commissionRates.setMinAmount(commissionRequest.getMinAmount());
            commissionRates.setUpdatedAt(LocalDateTime.now());
            commissionMapper.updateById(commissionRates);
            return Result.success();
        }catch (Exception e) {
            log.error("updateCommission: ", e);
            e.printStackTrace();
        }
        return Result.build(1,"Update commission failed");
    }

}
